﻿using LeSoft.Application.Bussiness;
using LeSoft.Application.Cache;
using LeSoft.Application.Code;
using LeSoft.Application.Entity;
using LeSoft.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LeSoft.Application.Web.Areas.FinancialManage.Controllers
{
    [HandleError]
    public class FeeItemsController : MvcControllerBase
    {

        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        [HandlerAction(OperationType.Visit)]
        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        [HandlerAction(OperationType.Visit)]
        public ActionResult Form()
        {
            return View();
        }


        #region 查看数据
        [HttpGet]
        [HandlerAction(OperationType.GetList, "费项设置", "GetTreeList")]
        public ActionResult GetTreeList()
        {
            var treeList = new List<TreeEntity>();
            treeList.Add(new TreeEntity() { hasChildren = true, id = "-1", text = "按计费方式查看", value = "-1", isexpand = true, complete = true, parentId = "0", Attribute = "Type", AttributeValue = "0" });
            treeList.Add(new TreeEntity() { hasChildren = true, id = "-2", text = "按统计类别查看", value = "-2", isexpand = true, complete = true, parentId = "0", Attribute = "Type", AttributeValue = "0" });
           // treeList.Add(new TreeEntity() { hasChildren = true, id = "-3", text = "按费项性质查看", value = "-3", isexpand = true, complete = true, parentId = "0", Attribute = "Type", AttributeValue = "0" });
            //treeList.Add(new TreeEntity() { hasChildren = true, id = "-4", text = "按税务项目查看", value = "-4", isexpand = true, complete = true, parentId = "0", Attribute = "Type", AttributeValue = "0" });
            treeList.Add(new TreeEntity() { hasChildren = false, id = "1", text = "周期性收费项目", value = "1", isexpand = true, complete = true, parentId = "-1", Attribute = "Type", AttributeValue = "1" });
            treeList.Add(new TreeEntity() { hasChildren = false, id = "2", text = "临时性收费项目", value = "2", isexpand = true, complete = true, parentId = "-1", Attribute = "Type", AttributeValue = "1" });
            treeList.Add(new TreeEntity() { hasChildren = false, id = "3", text = "滞纳金类收费项目", value = "3", isexpand = true, complete = true, parentId = "-1", Attribute = "Type", AttributeValue = "1" });
            var data = new DataItemCache().GetDataItemList("StatiscalCategory");         
            foreach (DataItemModel item in data)
            {
                TreeEntity tree = new TreeEntity();
                bool hasChildren = data.Count(t => t.ParentId == item.ItemDetailId) == 0 ? false : true;
                tree.id = item.ItemDetailId;
                tree.text = item.ItemName;
                tree.value = item.ItemValue;
                tree.title = item.SimpleSpelling;
                tree.parentId =item.ParentId=="0"?"-2": item.ParentId;
                tree.isexpand = true;
                tree.complete = true;
                tree.hasChildren = hasChildren;
                treeList.Add(tree);
            }
            return Content(treeList.TreeToJson());
        }

        [HttpGet]
        [HandlerAction(OperationType.GetList, "费项设置", "GetTreeFeeItemsList")]
        public ActionResult GetTreeFeeItemsList()
        {
            var treeList = new List<TreeEntity>();
            treeList.Add(new TreeEntity() { hasChildren = true, id = "2", text = "临时性收费项目", value = "2", isexpand = true, complete = true, parentId = "0" });
            var data = new FeeItemBLL().GetList();
            foreach (var item in data.Where(t => t.CalType == "2"))
            {
                TreeEntity tree = new TreeEntity();
                tree.id = item.Id;
                tree.text = item.Name;
                tree.value = item.Id;
                tree.parentId = "2";
                tree.isexpand = false;
                tree.complete = true;
                tree.hasChildren = false;
                treeList.Add(tree);
            }
            return Content(treeList.TreeToJson());
        }

        /// <summary>
        /// 查询费项
        /// </summary>
        /// <param name="queryJson"></param>
        /// <returns></returns>
        ///  树--checkbox
        [HttpGet]
        [HandlerAction(OperationType.GetList, "费项列表树", "GetItemsTreeList")]
        public ActionResult GetItemsTreeList(string queryJson)
        {
            var data = new FeeItemBLL().GetList();
            var existFeeItem =new List<RelationEnergyFeeItemEntity>();
            #region 条件筛选
            var queryParam = queryJson.ToJObject();
            if (!queryParam["CalType"].IsEmpty())
            {
                data = data.Where(t => t.CalType == queryParam["CalType"].ToString()).ToList();
            }
            if (!queryParam["EnergyMeterId"].IsEmpty())
            {
                existFeeItem = new EnergyBLL().GetRelationList(queryParam["EnergyMeterId"].ToString()).ToList();
            }
            #endregion     
            var treeList = new List<TreeEntity>();
            foreach (var item in data)
            {
                TreeEntity tree = new TreeEntity();
                tree.id = item.Id;
                tree.text = item.Name;
                tree.value = item.Id;
                tree.parentId = "0";
                tree.checkstate = existFeeItem.Count(t => t.FeeItemId == item.Id);
                tree.showcheck = true;
                tree.isexpand = true;
                tree.complete = true;
                tree.hasChildren = false;
                treeList.Add(tree);
            }
            return Content(treeList.TreeToJson());
        }
        [HttpGet]
        [HandlerAction(OperationType.GetList, "费项设置", "GetPageListJson")]
        public ActionResult GetPageListJson(Pagination pagination, string queryJson)
        {
            var watch = CommonHelper.TimerStart();
            var data = new FeeItemBLL().GetPageList(pagination, queryJson);
            var JsonData = new
            {
                rows = data,
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                costtime = CommonHelper.TimerEnd(watch)
            };
            return Content(JsonData.ToJson());
        }

        [HttpGet]
        [HandlerAction(OperationType.GetList, "费项设置", "GetFormJson")]
        public ActionResult GetFormJson(string keyValue)
        {
            var data = new FeeItemBLL().GetEntity("", keyValue);
            return Content(data.ToJson());
        }
        #endregion

        #region 验证数据

        [HttpGet]
        [HandlerAction(OperationType.Exist, "费项设置", "ExistCode")]
        public ActionResult ExistCode(string Code, string keyValue)
        {
            bool IsOk = new FeeItemBLL().ExistCode(Code, keyValue);
            return Content(IsOk.ToString());
        }

        [HttpGet]
        [HandlerAction(OperationType.Exist, "费项设置", "ExistName")]
        public ActionResult ExistName(string Name, string keyValue)
        {
            bool IsOk = new FeeItemBLL().ExistName(Name, keyValue);
            return Content(IsOk.ToString());
        }
        #endregion

        #region 提交数据

        [HttpPost]
        [ValidateAntiForgeryToken]
        [AjaxOnly]       
        [HandlerAction(OperationType.Delete, "费项设置", "RemoveForm")]
        public ActionResult RemoveForm(string keyValue)
        {
            new FeeItemBLL().RemoveForm(keyValue);
            return Success("删除成功。");
        }



        [HttpPost]
        [ValidateAntiForgeryToken]
        [AjaxOnly]  
        [HandlerAction(OperationType.Create, "费项设置", "SaveForm")]
        public ActionResult SaveForm(string keyValue, FeeItemEntity entity)
        {
            new FeeItemBLL().SaveForm(keyValue, entity);
            return Success("操作成功。");
        }
        #endregion
    }
}
